/**
 * 
 */
package com.alstudio.ui.module.main.fragment.discovery;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Handler;
import android.provider.CalendarContract.Events;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.pojo.module.user.User;
import com.alstudio.ui.module.user.UserHomePageAcitivty;
import com.alstudio.utils.activity.ALActivityManager;
import com.alstudio.utils.android.device.ALDisplayMetricsManager;
import com.alstudio.utils.formater.sex.ALSexFormatter;
import com.alstudio.utils.formater.time.ALTimeUtils;
import com.alstudio.utils.umeng.UmengUtils;
import com.alstudio.view.absadapter.ViewAdapter;
import com.loovee.imaohu.R;

/**
 * @author alonso lee
 * 
 */
public class DiscoveryGridItemAdapter extends ViewAdapter {

	private ArrayList<User> list = new ArrayList<User>();
	private final int COLUMN_COUNT = 3;

	private int iconRes = -1;

	private MainGridItemShowType type = MainGridItemShowType.MAIN_GRID_ITEM_FOR_DISCOVERY;

	/**
	 * 配置显示类型
	 * 
	 * @author alonso lee
	 * 
	 */
	public enum MainGridItemShowType {
		MAIN_GRID_ITEM_FOR_DISCOVERY, MAIN_GRID_ITEM_FOR_GALMOUR, MAIN_GRID_ITEM_FOR_RICH, MAIN_GRID_ITEM_FOR_SEXY,
	}

	/**
	 * 
	 */
	public DiscoveryGridItemAdapter(ArrayList<User> data) {
		// TODO Auto-generated constructor stub
		this.list = data;
	}

	/**
	 * @param type
	 *            the type to set
	 */
	public void setType(MainGridItemShowType type) {
		this.type = type;
	}

	/**
	 * @return the type
	 */
	public MainGridItemShowType getType() {
		return type;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.view.absadapter.ViewAdapter#getRealCount()
	 */
	@Override
	public int getRealCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	/**
	 * @param iconRes
	 *            the iconRes to set
	 */
	public void setIconRes(int iconRes) {
		this.iconRes = iconRes;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getCount()
	 */
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (list.size() % COLUMN_COUNT > 0)
			return list.size() / COLUMN_COUNT + 1;
		else
			return list.size() / COLUMN_COUNT;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getItem(int)
	 */
	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getItemId(int)
	 */
	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getView(int, android.view.View,
	 * android.view.ViewGroup)
	 */
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		View view = convertView;
		final ViewHolder holder;

		if (convertView == null) {
			view = ALLocalEnv
					.getLocalEnv()
					.getGlobalLayoutInflater()
					.inflate(R.layout.gridview_three_item_layout, parent, false);
			holder = new ViewHolder();
			initViewHolder(holder, view);

		} else {
			holder = (ViewHolder) view.getTag();
		}
		new Handler().post(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
//				setupItem(position, holder);

			}
		});

		setupItem(position, holder);
		return view;
	}

	private void initViewHolder(ViewHolder holder, View view) {
		holder.item1 = (RelativeLayout) view.findViewById(R.id.item1);
		holder.icon1 = (ImageView) view.findViewById(R.id.icon1);
		holder.nick_name1 = (TextView) view.findViewById(R.id.nick_name1);
		holder.time_icon1 = (ImageView) view.findViewById(R.id.time_icon1);
		holder.time1 = (TextView) view.findViewById(R.id.time1);
		holder.icons_layout1 = (LinearLayout) view
				.findViewById(R.id.icons_layout1);
		holder.sex1 = (ImageView) view.findViewById(R.id.sex1);
		holder.vauth1 = (ImageView) view.findViewById(R.id.vauth1);
		holder.top_img1 = (ImageView) view.findViewById(R.id.top_img1);

		holder.item2 = (RelativeLayout) view.findViewById(R.id.item2);
		holder.icon2 = (ImageView) view.findViewById(R.id.icon2);
		holder.nick_name2 = (TextView) view.findViewById(R.id.nick_name2);
		holder.time_icon2 = (ImageView) view.findViewById(R.id.time_icon2);
		holder.time2 = (TextView) view.findViewById(R.id.time2);
		holder.icons_layout2 = (LinearLayout) view
				.findViewById(R.id.icons_layout2);
		holder.sex2 = (ImageView) view.findViewById(R.id.sex2);
		holder.vauth2 = (ImageView) view.findViewById(R.id.vauth2);
		holder.top_img2 = (ImageView) view.findViewById(R.id.top_img2);

		holder.item3 = (RelativeLayout) view.findViewById(R.id.item3);
		holder.icon3 = (ImageView) view.findViewById(R.id.icon3);
		holder.nick_name3 = (TextView) view.findViewById(R.id.nick_name3);
		holder.time_icon3 = (ImageView) view.findViewById(R.id.time_icon3);
		holder.time3 = (TextView) view.findViewById(R.id.time3);
		holder.icons_layout3 = (LinearLayout) view
				.findViewById(R.id.icons_layout3);
		holder.sex3 = (ImageView) view.findViewById(R.id.sex3);
		holder.vauth3 = (ImageView) view.findViewById(R.id.vauth3);
		holder.top_img3 = (ImageView) view.findViewById(R.id.top_img3);

		holder.item4 = (RelativeLayout) view.findViewById(R.id.item4);
		holder.icon4 = (ImageView) view.findViewById(R.id.icon4);
		holder.nick_name4 = (TextView) view.findViewById(R.id.nick_name4);
		holder.time_icon4 = (ImageView) view.findViewById(R.id.time_icon4);
		holder.time4 = (TextView) view.findViewById(R.id.time4);
		holder.icons_layout4 = (LinearLayout) view
				.findViewById(R.id.icons_layout4);
		holder.sex4 = (ImageView) view.findViewById(R.id.sex4);
		holder.vauth4 = (ImageView) view.findViewById(R.id.vauth4);

		view.setTag(holder);
	}

	private void setupItem(int position, ViewHolder viewHodler) {
		if (list.size() > position * COLUMN_COUNT) {
			setItemLayoutParms(viewHodler.item1);
			viewHodler.item1.setVisibility(View.VISIBLE);
			setNick(viewHodler.nick_name1, list.get(position * COLUMN_COUNT));
			setLoginTimeStamp(viewHodler.time1,
					list.get(position * COLUMN_COUNT));
			setHead(viewHodler.icon1, list.get(position * COLUMN_COUNT));
			setIconRes(viewHodler.time_icon1);
			setupIconsLayout(viewHodler.sex1, viewHodler.vauth1,
					list.get(position * COLUMN_COUNT));
			addEvent(viewHodler.item1, list.get(position *
					 COLUMN_COUNT));
			setRightTopImg(position * COLUMN_COUNT, viewHodler.top_img1);
		} else {
			viewHodler.item1.setVisibility(View.INVISIBLE);
		}

		if (list.size() > position * COLUMN_COUNT + 1) {
			setItemLayoutParms(viewHodler.item2);
			viewHodler.item2.setVisibility(View.VISIBLE);
			setNick(viewHodler.nick_name2,
					list.get(position * COLUMN_COUNT + 1));
			setLoginTimeStamp(viewHodler.time2,
					list.get(position * COLUMN_COUNT + 1));
			setHead(viewHodler.icon2, list.get(position * COLUMN_COUNT + 1));
			setIconRes(viewHodler.time_icon2);
			setupIconsLayout(viewHodler.sex2, viewHodler.vauth2,
					list.get(position * COLUMN_COUNT + 1));
			addEvent(viewHodler.item2, list.get(position *
					 COLUMN_COUNT + 1));
			setRightTopImg(position * COLUMN_COUNT + 1, viewHodler.top_img2);
		} else {
			viewHodler.item2.setVisibility(View.INVISIBLE);
		}

		if (list.size() > position * COLUMN_COUNT + 2) {
			setItemLayoutParms(viewHodler.item3);
			viewHodler.item3.setVisibility(View.VISIBLE);
			setNick(viewHodler.nick_name3,
					list.get(position * COLUMN_COUNT + 2));
			setLoginTimeStamp(viewHodler.time3,
					list.get(position * COLUMN_COUNT + 2));
			setHead(viewHodler.icon3, list.get(position * COLUMN_COUNT + 2));
			setIconRes(viewHodler.time_icon3);

			setupIconsLayout(viewHodler.sex3, viewHodler.vauth3,
					list.get(position * COLUMN_COUNT + 2));
			addEvent(viewHodler.item3, list.get(position *
			 COLUMN_COUNT + 2));
			setRightTopImg(position * COLUMN_COUNT + 2, viewHodler.top_img3);
		} else {
			viewHodler.item3.setVisibility(View.INVISIBLE);
		}

		// if (list.size() > position * COLUMN_COUNT + 3) {
		// setItemLayoutParms(viewHodler.item4);
		// viewHodler.item4.setVisibility(View.VISIBLE);
		// setNick(viewHodler.nick_name4,
		// list.get(position * COLUMN_COUNT + 3));
		// setLoginTimeStamp(viewHodler.time4,
		// list.get(position * COLUMN_COUNT + 3));
		// setHead(viewHodler.icon4, list.get(position * COLUMN_COUNT + 3));
		// setIconRes(viewHodler.time_icon4);
		//
		// setupIconsLayout(viewHodler.sex4, viewHodler.vauth4,
		// list.get(position * COLUMN_COUNT + 3));
		// // onClickEvent(viewHodler.num_img4, list.get(position *
		// // COLUMN_COUNT + 2));
		// } else {
		// viewHodler.item4.setVisibility(View.INVISIBLE);
		// }
	}

	private void setItemLayoutParms(RelativeLayout itemLayout) {
		int width = ALDisplayMetricsManager.getScreenWidth(ALLocalEnv
				.getLocalEnv()) / COLUMN_COUNT - 5;
		ViewGroup.LayoutParams para = itemLayout.getLayoutParams();
		para.height = width;
		para.width = width;
		itemLayout.setLayoutParams(para);
	}

	// 设置头像
	private void setHead(ImageView head, User u) {
		// int width = ALDisplayMetricsManager.getScreenWidth(ALLocalEnv
		// .getLocalEnv()) / COLUMN_COUNT -5;
		// ViewGroup.LayoutParams para = head.getLayoutParams();
		// para.height = width;
		// para.width = width;
		// head.setLayoutParams(para);
		//
		// head.setScaleType(ScaleType.CENTER_CROP);

		ALLocalEnv.getLocalEnv().displayImagePluginServerlet(
				u.getImageFileId(), head,
				ALLocalEnv.getLocalEnv().getNormalRectImageDisplayOption());

	}

	private void setNick(TextView nick_name, User u) {
		nick_name.setText(u.getNick());
	}

	private void setRightTopImg(int pos, ImageView v) {
		if (type != MainGridItemShowType.MAIN_GRID_ITEM_FOR_DISCOVERY) {
			switch (pos) {
			case 0:
				v.setImageResource(R.drawable.hot_user_icon_top1);
				v.setVisibility(View.VISIBLE);
				break;
			case 1:
				v.setImageResource(R.drawable.hot_user_icon_top2);
				v.setVisibility(View.VISIBLE);
				break;
			case 2:
				v.setVisibility(View.VISIBLE);
				v.setImageResource(R.drawable.hot_user_icon_top3);
				break;
			default:
				v.setVisibility(View.GONE);
				break;
			}
		} else {
			v.setVisibility(View.GONE);
		}

	}

	private void setLoginTimeStamp(TextView time, User u) {
		switch (type) {
		case MAIN_GRID_ITEM_FOR_DISCOVERY:
			time.setText(ALTimeUtils.formatTime(u.getLastLoginTimeStamp()));
			break;
		case MAIN_GRID_ITEM_FOR_GALMOUR:
			// 魅力榜
			time.setText((u.getGlamour()));
			break;
		case MAIN_GRID_ITEM_FOR_RICH:
			// 富豪榜
			time.setText((u.getGold()));
			break;
		case MAIN_GRID_ITEM_FOR_SEXY:
			// 女神榜
			time.setText((u.getPoint()));
			break;
		default:
			break;

		}
	}

	private void setIconRes(ImageView v) {
		if (iconRes != -1) {
			v.setImageResource(iconRes);
		}
	}

	private void setupIconsLayout(ImageView sex, ImageView vauth, User u) {

		sex.setImageResource(ALSexFormatter.transSexDrawable(u.getSex()));
		if (u.isVauthed()) {
			vauth.setImageResource(R.drawable.find_user_icon_vip_big);
		} else {
			vauth.setImageBitmap(null);
		}
	}
	
	private void addEvent(View v, final User user) {
		v.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				if (null != user) {
					Intent intent = new Intent(ALActivityManager.getALActivityManager()
							.getCurrentActivity(), UserHomePageAcitivty.class);
					intent.putExtra("user", user);
					ALActivityManager.getALActivityManager().getCurrentActivity()
							.startActivity(intent);
				}
			}
		});
	}

	class ViewHolder {
		public RelativeLayout item1;
		public LinearLayout icons_layout1;
		public ImageView icon1;
		public TextView nick_name1;
		public ImageView time_icon1;
		public TextView time1;
		public ImageView sex1;
		public ImageView vauth1;
		public ImageView top_img1;

		public RelativeLayout item2;
		public LinearLayout icons_layout2;
		public ImageView icon2;
		public TextView nick_name2;
		public ImageView time_icon2;
		public TextView time2;
		public ImageView sex2;
		public ImageView vauth2;
		public ImageView top_img2;

		public RelativeLayout item3;
		public LinearLayout icons_layout3;
		public ImageView icon3;
		public TextView nick_name3;
		public ImageView time_icon3;
		public TextView time3;
		public ImageView sex3;
		public ImageView vauth3;
		public ImageView top_img3;

		public RelativeLayout item4;
		public LinearLayout icons_layout4;
		public ImageView icon4;
		public TextView nick_name4;
		public ImageView time_icon4;
		public TextView time4;
		public ImageView sex4;
		public ImageView vauth4;

	}

}
